import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from 'element-plus';


// 创建路由规则
const routes:any = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/regire',
    name: 'regire',
    component: () => import('../views/RegixView.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'prodList',
        component: () => import('../views/prodListView.vue')
      },
      {
        path: 'brandList',
        component: () => import('../views/BrandList.vue')
      },
      {
        path: 'classify',
        component: () => import('../views/ClassifyView.vue')
      },
      {
        path: 'saveProd',
        component: () => import('../views/SaveProd.vue')
      },
      {
        path: 'orderList',
        component: () => import('../views/OrderListView.vue')
      },
      {
        path: 'prodInfo/:prodId',
        component: () => import('../views/prodInfoView.vue')
      },
      {
        path: 'activejoin',
        component: () => import('../views/ActiveView.vue')
      },
      {
        path: 'groupjoin',
        component: () => import('../views/GrouJoinView.vue')
      },
      {
        path: 'group',
        component: () => import('../views/GroupView.vue')
      },
      {
        path: 'myGroup',
        component: () => import('../views/ActiveJoinView.vue')
      },
      {
        path: 'ruleList',
        component: () => import('../views/RuleList.vue')
      },
      {
        path: 'recordList',
        component: () => import('../views/RecordView.vue')
      },
      {
        path: 'saveView',
        component: () => import('../views/SaveView.vue')
      },
      {
        path: 'updateView/:activeId',
        component: () => import('../views/UpdateView.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/line',
    name: 'line',
    component: () => import('../views/DemoLineView.vue')
  },
];
 
// 创建router实例
const router = createRouter({
  history: createWebHistory(),
  routes
});
 
// 全局前置守卫
router.beforeEach((to, from, next) => {

    // 检查用户是否认证，例如检查token
    let token = window.sessionStorage.getItem('token');
  if (to.name == 'login'|| to.name=='regire') {
        next()
        return;
    }
    if(token==null) {
      next({ path: '/login', query: { redirect: to.fullPath } });
      ElMessage.error("亲请先登录")
    }
    else {
      next();
    }
});

export default router
